import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screen_util.dart';
import 'package:image_picker/image_picker.dart';
import 'package:provider/provider.dart';
import 'package:study_online/global/colors.dart';

import '../MyViewModel.dart';

showImgDialog(BuildContext context) async {
  ScreenUtil.init(
    BoxConstraints(
        maxWidth: MediaQuery.of(context).size.width,
        maxHeight: MediaQuery.of(context).size.height),
    designSize: Size(1080, 2340),
  );
  showModalBottomSheet(
    context: context,
    builder: (BuildContext context) {
      return Container(
        color: Color(0xff737373),
        child: Container(
          width: double.infinity,
          height: ScreenUtil().setHeight(500),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(15), topRight: Radius.circular(15)),
          ),
          padding: EdgeInsets.only(top: ScreenUtil().setWidth(30)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              InkWell(
                onTap: () async {
                  File pickedFile = await ImagePicker.pickImage(
                    source: ImageSource.camera,
                  );
                  final File file = File(pickedFile.path);
                  Provider.of<MyViewModel>(context,listen: false).setImg(file);
                },
                child: Container(
                  height: ScreenUtil().setHeight(142),
                  color: Colors.white,
                  child: Center(
                    child: Text(
                      '拍照',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(50),
                        color: Colors.black,
                      ),
                    ),
                  ),
                ),
              ),
              Divider(
                height: 0.5,
                color: Colors.black12,
              ),
              InkWell(
                onTap: () async {
                  final pickedFile = await ImagePicker.pickImage(source: ImageSource.gallery);
                  final File file = File(pickedFile.path);
                  context.read<MyViewModel>().setImg(file);
                },
                child: Container(
                  height: ScreenUtil().setHeight(142),
                  color: Colors.white,
                  child: Center(
                    child: Text(
                      '从相册中选择',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(50),
                        color: Colors.black,
                      ),
                    ),
                  ),
                ),
              ),
              Container(
                height: ScreenUtil().setHeight(40),
                color: AppColors.pageBackground,
              ),
              Expanded(
                child: InkWell(
                  onTap: (){
                    Navigator.pop(context);
                  },
                  child: Center(
                    child: Text(
                      '取消',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(50),
                        color: Colors.black,
                      ),
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      );
    },
  );
}

class InputDialog1 extends Dialog {
  const InputDialog1({Key key,this.content}) : super(key: key);
  final String content;
  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(1080, 2340),
    );
    FocusNode _focusNode = new FocusNode();
    TextEditingController _controller = new TextEditingController();
    void _onCancel() {
      Navigator.pop(context);
    }

    void _onSure() {
      if(content=='专业名称')
        context.read<MyViewModel>().setMagor(_controller.text);
      else if(content=='学院名称')
        context.read<MyViewModel>().setMagor(_controller.text);
      else if(content=='姓名')
        context.read<MyViewModel>().setName(_controller.text);
      else if(content=='手机号')
        context.read<MyViewModel>().setPhone(_controller.text);
      else if(content=='QQ号')
        context.read<MyViewModel>().setQQ(_controller.text) ;
      else
        context.read<MyViewModel>().setWeChat(_controller.text) ;
      Navigator.pop(context);
    }

    return new Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      child: Center(
        //保证控件居中效果
        child: IntrinsicHeight(
          child: Container(
            width: ScreenUtil().screenWidth - 60,
            height: 140,
            decoration: ShapeDecoration(
              color: Color(0xffffffff),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(8.0),
                ),
              ),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                Container(
                  decoration: BoxDecoration(
                    color: AppColors.pageBackground,
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                  ),
                  padding: EdgeInsets.only(left: ScreenUtil().setWidth(40)),
                  width: ScreenUtil().screenWidth - 100,
                  child: TextField(
                    focusNode: _focusNode,
                    controller: _controller,
                    cursorColor: Color(0xff2DCA97),
                    decoration: InputDecoration(
                      suffixIcon: IconButton(
                        icon: Icon(
                          Icons.close,
                          color: Color(0xffBEBEBE),
                        ),
                        onPressed: () {
                          _controller.clear();
                        },
                      ),
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.all(
                            Radius.circular(ScreenUtil().setHeight(20))),
                      ),
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(color: Colors.transparent),
                      ),
                      focusedBorder: UnderlineInputBorder(
                        borderSide: BorderSide(color: Colors.transparent),
                      ),
                      hintText: '请输入$content',
                      hintStyle: TextStyle(
                        color: Color(0xffBEBEBE),
                        fontSize: ScreenUtil().setSp(50),
                      ),
                      labelStyle: TextStyle(
                          color: Color(0xffBEBEBE),
                          fontSize: ScreenUtil().setSp(50)),
                    ),
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    InkWell(
                      child: Container(
                        width: ScreenUtil().setWidth(250),
                        height: ScreenUtil().setHeight(110),
                        decoration: BoxDecoration(
                          color: AppColors.pageBackground,
                          borderRadius: BorderRadius.all(
                              Radius.circular(ScreenUtil().setHeight(75))),
                        ),
                        child: Center(
                          child: Text(
                            '取消',
                            style: TextStyle(
                              fontSize: ScreenUtil().setSp(50),
                              color: Colors.black,
                            ),
                          ),
                        ),
                      ),
                      onTap: () {
                        _onCancel();
                      },
                    ),
                    InkWell(
                      child: Container(
                        width: ScreenUtil().setWidth(250),
                        height: ScreenUtil().setHeight(110),
                        decoration: BoxDecoration(
                          color: AppColors.primary,
                          borderRadius: BorderRadius.all(
                              Radius.circular(ScreenUtil().setHeight(75))),
                        ),
                        child: Center(
                            child: Text(
                              '确认',
                              style: TextStyle(
                                fontSize: ScreenUtil().setSp(50),
                                color: Colors.white,
                              ),
                            )),
                      ),
                      onTap: () {
                        _onSure();
                      },
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

showDataDialog(BuildContext context) async {
  ScreenUtil.init(
    BoxConstraints(
        maxWidth: MediaQuery.of(context).size.width,
        maxHeight: MediaQuery.of(context).size.height),
    designSize: Size(1080, 2340),
  );
  showModalBottomSheet(
    context: context,
    builder: (BuildContext context) {
      return Container(
        color: Color(0xff737373),
        child: Container(
          width: double.infinity,
          height: ScreenUtil().setHeight(700),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(15), topRight: Radius.circular(15)),
          ),
          padding: EdgeInsets.only(top: ScreenUtil().setWidth(30)),
          child: CupertinoDatePicker(
            mode: CupertinoDatePickerMode.date, // 展示模式, 默认为 dateAndTime
            initialDateTime: DateTime(2000, 1, 1), // 默认选中日期
            minimumDate: DateTime(1960, 1, 1), // 最小可选日期
            maximumDate: DateTime(2021, 10, 10), // 最大可选日期
            minuteInterval: 10, // 分钟间隔
            use24hFormat: true, // 是否采用24小时制
            backgroundColor: Colors.white, // 背景色
            onDateTimeChanged: (dayTime){
              print("选择日期时间 $dayTime");
              context.read<MyViewModel>().setBirthDay(dayTime.toString().substring(0,10));
            },
          ),
        ),
      );
    },
  );
}